var gameEnum = require("GameEnum");

cc.Class({
    extends: cc.Component,

    properties: {
        label: {
            default: null,
            type: cc.Label
        },
        // defaults, set visually when attaching this script to the Canvas
        text: 'Hello, World222!'
    },

    // use this for initialization
    onLoad: function () {
        this.initView();
        this.initData();
    },

    // called every frame
    update: function (dt) {
        this.refreshRobotLocal();
        this.refreshTokenView();
    },
    /**
     * 初始化界面
     */
    initView() {
        this.label.string = this.text;
        this.initMapView();
        this.initBtnsView();
        this.initTokenView();
    },
    /**
     * 初始化界面--地图
     */
    initMapView() {
        // this.mapNode = cc.find("MapNode", this.node);
        this.mapLayout = cc.find("mapFrame/mapLayout", this.node).getComponent("cc.Layout");
        this.landNode = cc.find("mapFrame/landNode", this.node).getComponent("LandNode");
        this.robot = cc.find("mapFrame/robot", this.node).getComponent("RobotNode");
    },
    /**
     * 初始化界面--按钮组
     */
    initBtnsView() {

        var that = this;
        this.button1 = cc.find("button1", this.node).getComponent("cc.Button");
        this.button2 = cc.find("button2", this.node).getComponent("cc.Button");
        this.button3 = cc.find("button3", this.node).getComponent("cc.Button");
        this.button4 = cc.find("button4", this.node).getComponent("cc.Button");

        this.btnAdvance = cc.find("btns/btnAdvance", this.node).getComponent("cc.Button");
        this.btnPick = cc.find("btns/btnPick", this.node).getComponent("cc.Button");
        this.btnThrow = cc.find("btns/btnThrow", this.node).getComponent("cc.Button");
        this.btnTurnLeft = cc.find("btns/btnTurnLeft", this.node).getComponent("cc.Button");
        this.btnTurnRight = cc.find("btns/btnTurnRight", this.node).getComponent("cc.Button");
        this.btnLeap = cc.find("btns/btnLeap", this.node).getComponent("cc.Button");
        this.btnFunc = cc.find("btns/btnFunc", this.node).getComponent("cc.Button");
        this.btnRemove = cc.find("btns/btnRemove", this.node).getComponent("cc.Button");

        this.button1.node.on(cc.Node.EventType.TOUCH_END, function () {
            that.button1Click();
        }, this.button1.node);
        this.button2.node.on(cc.Node.EventType.TOUCH_END, function () {
            that.button2Click();
        }, this.button2.node);
        this.button3.node.on(cc.Node.EventType.TOUCH_END, function () {
            that.button3Click();
        }, this.button3.node);
        this.button4.node.on(cc.Node.EventType.TOUCH_END, function () {
            that.button4Click();
        }, this.button4.node);

        this.btnAdvance.node.on(cc.Node.EventType.TOUCH_END, function () {
            that.btnAdvanceClick();
        }, this.btnAdvance.node);

        this.btnPick.node.on(cc.Node.EventType.TOUCH_END, function () {
            that.btnPickClick();
        }, this.btnPick.node);
        this.btnThrow.node.on(cc.Node.EventType.TOUCH_END, function () {
            that.btnThrowClick();
        }, this.btnThrow.node);
        this.btnTurnLeft.node.on(cc.Node.EventType.TOUCH_END, function () {
            that.btnTurnLeftClick();
        }, this.btnTurnLeft.node);
        this.btnTurnRight.node.on(cc.Node.EventType.TOUCH_END, function () {
            that.btnTurnRightClick();
        }, this.btnTurnRight.node);
        this.btnLeap.node.on(cc.Node.EventType.TOUCH_END, function () {
            that.btnLeapClick();
        }, this.btnLeap.node);
        this.btnFunc.node.on(cc.Node.EventType.TOUCH_END, function () {
            that.btnFuncClick();
        }, this.btnFunc.node);
        this.btnRemove.node.on(cc.Node.EventType.TOUCH_END, function () {
            that.btnRemoveClick();
        }, this.btnRemove.node);
    },
    /**
     * 初始化界面--动作
     */
    initTokenView() {
        this.tokenLayout = cc.find("tokenLayout", this.node).getComponent("cc.Layout");
        this.tokenItem = cc.find("item", this.node);
    },
    /**
     * 初始化数据
     */
    initData() {
        this.landArr = new Array();
        this.tokenArr = new Array();
        this.preTokenArrCount = -1;
        this.resetActionArr();

        this.rowNum = 7;
        this.allNum = 70;

        for (var i = 0; i < this.allNum; i++) {
            var tmpLand = cc.instantiate(this.landNode);
            var tmpNode = cc.instantiate(this.landNode.node);
            tmpLand.node = tmpNode;
            // tmpNode.node.active = true;
            this.mapLayout.node.addChild(tmpNode);
            this.landArr.push(tmpLand);
        }
    },

    refreshRobotLocal() {
        this.label.string = this.robot.dir;
    },

    refreshTokenView() {
        if (this.preTokenArrCount != this.tokenArr.length) {
            this.tokenLayout.node.removeAllChildren();
            for (var i = 0; i < this.tokenArr.length; i++) {
                var tmpNode = cc.instantiate(this.tokenItem);
                // tmpNode.active = true;
                // console.log(tmpNode);
                var tmpLabel = cc.find("label", tmpNode).getComponent("cc.Label");
                tmpLabel.string = this.tokenArr[i].value;
                this.tokenLayout.node.addChild(tmpNode);
            }
            console.log(this.tokenArr);
        }
        this.preTokenArrCount = this.tokenArr.length;
    },

    resetActionArr: function () {
        this.actionArr = new Array();
        var showAction = cc.show();
        this.actionArr.push(showAction);
    },

    button1Click: function () {
        console.log("button1 click");
    },
    button2Click: function () {
        console.log("button2 click");
    },
    button3Click: function () {
        cc.log("button3 click");
        this.resetActionArr();
        if (this.tokenArr.length == 0) {
            console.log("no operation");
            return;
        }

        for (var i = 0; i < this.tokenArr.length; i++) {
            switch (this.tokenArr[i].type) {
                case gameEnum.TokenTypeEnum.TOKEN_ADVANCE:
                    this.advance();
                    var action = cc.moveTo(0.5, this.robot.pointX, this.robot.pointY);
                    this.actionArr.push(action);
                    break;
                case gameEnum.TokenTypeEnum.TOKEN_PICK:
                    this.pick();
                    break;
                case gameEnum.TokenTypeEnum.TOKEN_THROW:
                    console.log("TOKEN_THROW");
                    break;
                case gameEnum.TokenTypeEnum.TOKEN_TURN_LEFT:
                    this.turnLeft();
                    break;
                case gameEnum.TokenTypeEnum.TOKEN_TURN_RIGHT:
                    this.turnRight();
                    break;
                case gameEnum.TokenTypeEnum.TOKEN_LEAP:
                    this.leap();
                    break;
                default:
                    console.log("default");
                    break;
            }
        }

        if (this.actionArr.length <= 1) {
            console.log("this.actionArr.length <= 1");
            return;
        }
        var seq = cc.sequence(this.actionArr);
        this.robot.node.runAction(seq);

    },

    button4Click: function () {
        console.log("button4 click");
    },

    btnAdvanceClick: function () {
        var token = new gameEnum.Token(gameEnum.TokenTypeEnum.TOKEN_ADVANCE, gameEnum.TokenTypeEnum.properties[gameEnum.TokenTypeEnum.TOKEN_ADVANCE].str)
        this.addToken(token);
    },
    btnPickClick: function () {
        var token = new gameEnum.Token(gameEnum.TokenTypeEnum.TOKEN_PICK, gameEnum.TokenTypeEnum.properties[gameEnum.TokenTypeEnum.TOKEN_PICK].str)
        this.addToken(token);
    },
    btnThrowClick: function () {
        var token = new gameEnum.Token(gameEnum.TokenTypeEnum.TOKEN_THROW, gameEnum.TokenTypeEnum.properties[gameEnum.TokenTypeEnum.TOKEN_THROW].str)
        this.addToken(token);
    },
    btnTurnLeftClick: function () {
        var token = new gameEnum.Token(gameEnum.TokenTypeEnum.TOKEN_TURN_LEFT, gameEnum.TokenTypeEnum.properties[gameEnum.TokenTypeEnum.TOKEN_TURN_LEFT].str)
        this.addToken(token);
    },
    btnTurnRightClick: function () {
        var token = new gameEnum.Token(gameEnum.TokenTypeEnum.TOKEN_TURN_RIGHT, gameEnum.TokenTypeEnum.properties[gameEnum.TokenTypeEnum.TOKEN_TURN_RIGHT].str)
        this.addToken(token);
    },
    btnLeapClick: function () {
        var token = new gameEnum.Token(gameEnum.TokenTypeEnum.TOKEN_LEAP, gameEnum.TokenTypeEnum.properties[gameEnum.TokenTypeEnum.TOKEN_LEAP].str)
        this.addToken(token);
    },
    btnFuncClick: function () {
        var token = new gameEnum.Token(gameEnum.TokenTypeEnum.TOKEN_FUNC, gameEnum.TokenTypeEnum.properties[gameEnum.TokenTypeEnum.TOKEN_FUNC].str)
        this.addToken(token);
    },
    btnRemoveClick: function () {
        this.tokenArr.pop();
    },

    addToken(token) {
        this.tokenArr.push(token);
    },
    advance: function () {
        switch (this.robot.dir) {
            case gameEnum.DirEnum.DIR_UP:
                if (this.robot.localY <= 0) {
                    console.log("this.robot.localY <= 0 ");
                    break;
                }
                this.robot.localY--;
                break;
            case gameEnum.DirEnum.DIR_DOWN:
                if (this.robot.localY >= this.allNum/this.rowNum) {
                    console.log("this.robot.localY >= this.allNum/this.rowNum ");
                    break;
                }
                this.robot.localY++;
                break;
            case gameEnum.DirEnum.DIR_LEFT:

                if (this.robot.localX <= 0) {
                    console.log("this.robot.localX <= 0 ");
                    break;
                }
                this.robot.localX--;
                break;
            case gameEnum.DirEnum.DIR_RIGHT:
                if (this.robot.localX >= this.rowNum - 1) {
                    console.log("this.robot.localX >=this.rowNum ");
                    break;
                }
                this.robot.localX++;
                break;
            default:
                break;
        }

        this.robot.updatePointByLocal(this.landArr, this.rowNum);

    },

    turnLeft: function () {
        var count = this.robot.dir - gameEnum.DirEnum.DIR_DEFAULT;
        var modNum = count % gameEnum.DirEnum.DIR_COUNT;
        modNum++;
        this.robot.dir = modNum + gameEnum.DirEnum.DIR_DEFAULT;

        var action = cc.rotateBy(0.5, -90);
        // var action = cc.moveTo(0.5, this.robot.pointX, this.robot.pointY);
        this.actionArr.push(action);

    },

    turnRight: function () {
        var count = this.robot.dir - gameEnum.DirEnum.DIR_DEFAULT;
        count += gameEnum.DirEnum.DIR_COUNT * gameEnum.DirEnum.DIR_COUNT;
        count--;
        var modNum = count % gameEnum.DirEnum.DIR_COUNT;
        if (modNum == 0) {
            modNum += gameEnum.DirEnum.DIR_COUNT;
        }
        this.robot.dir = modNum + gameEnum.DirEnum.DIR_DEFAULT;
        var action = cc.rotateBy(0.5, 90);
        this.actionArr.push(action);
    },
    leap: function () {
        this.advance();
        var action = cc.jumpTo(0.5, this.robot.pointX, this.robot.pointY, 50, 4);
        this.actionArr.push(action);
        this.robot.localZ++;
    },
    pick: function () {
        var pointIndex = this.rowNum * this.robot.localY + this.robot.localX;
        this.landArr[pointIndex].node.setScale(0.5);
    }

});
